function fig = fm_advanced(varargin)
% FM_ADVANCED create GUI for advanced settings
%
% HDL = FM_ADVANCED()
%
%Author:    Federico Milano
%Date:      04-Jul-2008
%Version:   1.0.0
%
%E-mail:    federico.milano@ucd.ie
%Web-site:  faraday1.ucd.ie/psat.html
%
% Copyright (C) 2002-2019 Federico Milano

global Settings Theme Fig

if nargin
  switch varargin{1}
   case 'iternum'

    if isempty(str2num(get(gcbo,'String')))
      set(gcbo,'String',num2str(Settings.pv2pqniter));
      fm_disp('The iteration number cannot be empty.',2)
    else
      Settings.pv2pqniter = round(str2num(get(gcbo,'String')));
      fm_disp(['Iteration number set to ',num2str(Settings.pv2pqniter)])
    end

  end
  return
end

if ishandle(Fig.advanced), figure(Fig.advanced), return, end

N = 10.75;
D = (N+3)*0.039+0.0244;
d1 = 1.1*1/(2*N+3);
d2 = 1.2*d1;
d3 = 0.95*d1;

h0 = figure('Color',Theme.color01, ...
            'Units', 'normalized', ...
            'ColorMap', [], ...
            'CreateFcn','Fig.advanced = gcf;', ...
            'DeleteFcn','Fig.advanced = -1;', ...
            'FileName','fm_advanced', ...
            'MenuBar','none', ...
            'Name','Advanced Settings', ...
            'NumberTitle','off', ...
            'PaperPosition',[18 180 576 432], ...
            'PaperType','A4', ...
            'PaperUnits','points', ...
            'Position',sizefig(0.55,D), ...
            'RendererMode','manual', ...
            'Tag','Advanced', ...
            'ToolBar','none', ...
            'UserData',Settings);

% Menu File
h1 = uimenu('Parent',h0, ...
            'Label','File', ...
            'Tag','MenuFile');
h2 = uimenu('Parent',h1, ...
            'Callback','close(gcf);', ...
            'Label','Save and Exit', ...
            'Tag','OTV', ...
            'Accelerator','s');
h2 = uimenu('Parent',h1, ...
            'Callback','Settings = get(gcf,''UserData''); close(gcf);', ...
            'Label','Discard and Exit', ...
            'Tag','NetSett', ...
            'Accelerator','x', ...
            'Separator','on');

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'ForegroundColor',Theme.color03, ...
               'Position',[0.038 d1 0.92 1-2*d1], ...
               'Style','frame', ...
               'Tag','Frame1');

% settings

if strcmp(Settings.platform,'MAC')
  aligntxt = 'center';
  dm = 0.015;
else
  aligntxt = 'left';
  dm = 0.0;
end

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','Settings.resetangles = get(gcbo,''Value'');', ...
               'Position',[0.078  20*d3  2*0.43  d2], ...
               'String','Reset pre-fault bus angles after fault clearing', ...
               'Style','checkbox', ...
               'Tag','CheckboxReset', ...
               'Value',Settings.resetangles);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','Settings.switch2nr = get(gcbo,''Value'');', ...
               'Position',[0.078  18.5*d3  2*0.43  d2], ...
               'String','Switch back to the NR method for small mismatches', ...
               'Style','checkbox', ...
               'Tag','CheckboxSwitch', ...
               'Value',Settings.switch2nr);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','Settings.donotask = get(gcbo,''Value'');', ...
               'Position',[0.078  17*d3  2*0.43  d2], ...
               'String','Apply current options without asking for confirmation', ...
               'Style','checkbox', ...
               'Tag','CheckboxAsk', ...
               'Value',Settings.donotask);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','Settings.locksnap = get(gcbo,''Value'');', ...
               'Position',[0.078  15.5*d3  2*0.43  d2], ...
               'String','Skip updating of snapshots after power flow analysis', ...
               'Style','checkbox', ...
               'Tag','CheckboxSnap', ...
               'Value',Settings.locksnap);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','Settings.vs = get(gcbo,''Value'');', ...
               'Position',[0.078  14*d3  2*0.43  d2], ...
               'String','Enable Jacobian trajectory tracking (deprecated & undocumented)', ...
               'Style','checkbox', ...
               'Tag','CheckboxVS', ...
               'Value',Settings.vs);
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'HorizontalAlignment','left', ...
               'Position',[0.23   12.5*d3-dm  0.63  d2], ...
               'String','Number of PF iterations before switching PV to PQ', ...
               'Style','text', ...
               'Tag','StaticText13');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'Callback','fm_advanced iternum', ...
               'ForegroundColor',Theme.color05, ...
               'FontName',Theme.font01, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.078   12.5*d3   0.14   d2], ...
               'String',num2str(Settings.pv2pqniter), ...
               'Style','edit', ...
               'Tag','EditTextDelta');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','Settings.multipvswitch = get(gcbo,''Value'');', ...
               'Position',[0.078  11*d3  2*0.43  d2], ...
               'String','Allow multiple simultaneous PV to PQ switches', ...
               'Style','checkbox', ...
               'Tag','CheckboxVS', ...
               'Value',Settings.multipvswitch);

if strcmp(Settings.platform,'MAC'), d2 = 1.35*d2; end

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color03, ...
               'Callback','close(gcf);', ...
               'FontWeight','bold', ...
               'ForegroundColor',Theme.color09, ...
               'Position',[0.558     2*d1    0.16   d2], ...
               'String','OK', ...
               'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','Settings = get(gcf,''UserData''); close(gcf);', ...
               'Position',[0.754     2*d1   0.16  d2], ...
               'String','Cancel', ...
               'Tag','Pushbutton5');

if nargout > 0, fig = h0; end
